<?php
require 'config/dataSource.php';
if (isset($_POST['resetButton'], $_POST['email'])) {
    $email = mysql_real_escape_string($_POST['email']);
    $email = strip_tags($email);
    if (!preg_match("/@.*spsu\.edu$/", $email)) {
        $display = "You must provide a SPSU email.";
        
    } else {
        $userEmailQuery = mysql_query("SELECT email FROM user");
        while ($row = mysql_fetch_assoc($userEmailQuery)) {
                if ($row['email'] == $email) {
                    $to = $email;
                    $subject = "Waggle Project Team 5 Reset Password";
                    $body = "This email is only used to reset your account password on Waggle Forum." . PHP_EOL;
                    $body.="Click on the link below to reset your account password on Waggle Forum." . PHP_EOL;
                    $body.="http://localhost/SWE_3613_Waggle_Project_2/password.php?email=" . sha1($email) . PHP_EOL;
                    $body.="Regards," . PHP_EOL;
                    $body.="Team 5 Waggle Project" . PHP_EOL;

                    $headers = "From: root@localhost.com";
                    mail($to, $subject, $body, $headers);
                    echo ("<SCRIPT LANGUAGE='JavaScript'>
                            window.alert('A link has been send to your email ". $email."')
                            window.location.href='index.php';
                            </SCRIPT>");
                    
                } 
                    
        }
        $display = "Email not registered. Please sign up.";
        
    }
}
?>
<html>
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="css/newAccount.css">
        <title>Password Reset Page</title>
    </head>
    <body onload="document.passwordForm.reset()" style="background: url(images/background.jpg);">
        <div id="wrapper">

            <div id="head">

                <span>Return to log in page? <a class="newAccount" href="index.php">Go Home!</a></span>

            </div>
            <div id="container">
                <img src="images/spsu.png" style="height: 30%; width: 40%; margin-left: 30%;">

                <div id="discussion">

                    <h3 class="WaggleTopic">Password Reset</h3>

                    <hr/>
                </div>
                <form id="form1" method="post" name ="paswordForm">
                    <table style="border: 0px;" cellspacing="20">
                        </tr>
                        <tr>
                        </tr>
                        <tr>
                        </tr>
                        <tr>
                            <td align="center"><label class="label">SPSU Email</label></td>
                            <td><input id ="user" name="email" type="text" size="30"></td>
                        </tr>
                        <tr>
                        </tr>
                        <tr>
                        </tr>
                        </tr>
                        <tr>
                        </tr>
                        <tr>
                        </tr>
                        <tr>
                            <td colspan="2" align="center"><input id="signIn" type="submit" name="resetButton" value="Reset Password"/></td>

                        </tr>       
                        <tr>
                            <td colspan="2" id="error">

                                <?php
                                if (isset($display)) {

                                    echo $display;
                                }
                                ?>


                            </td>
                        </tr>

                    </table>
                </form>
            </div>
        </div>
    </body>
</html>